package com.dq.domain.sys.menu;


import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单
 *
 * @author gewenzhen
 */
@Data
@Entity
@ApiModel(value = "菜单")
public class SysMenu extends BaseEntity implements VO.ToVO {

    @ManyToOne
    @ApiModelProperty(value = "父菜单", hidden = true)
    protected SysMenu parent;

    @ApiModelProperty(value = "菜单名称")
    protected String title;
    @ApiModelProperty(value = "菜单图标")
    protected String icon;
    @ApiModelProperty(value = "菜单权限标识")
    protected String resources;
    @ApiModelProperty(value = "排序")
    @NotNull(message = "排序不能为空")
    protected Integer orderNum;

    @ApiModelProperty(value = "类型")
    @Enumerated(EnumType.ORDINAL)
    protected Type type;
    @ApiModelProperty(value = "是否系统菜单")
    protected Boolean sys;
    @Enumerated(EnumType.ORDINAL)
    @ApiModelProperty(value = "状态")
    protected Status status;
    @ApiModelProperty("菜单层级")
    protected Long level;

    @ApiModelProperty(value = "菜单所属[平台/门店, 默认平台]")
    @Enumerated(EnumType.ORDINAL)
    @NotNull
    protected Genre genre;

    public enum Type {
        list("目录"), param("菜单"), button("按钮");
        Type(String name) {
            this.name = name;
        }
        public String name;
    }

    public enum Genre {
        boss("boss平台"), org("门店");
        Genre(String name) {
            this.name = name;
        }
        public String name;
    }

    public enum Status {
        off("禁用"), on("启用"),
        ;
        Status(String name) {
            this.name = name;
        }
        public String name;
    }

    public SysMenu(){

    }

    public SysMenu(Genre genre, Long level,Integer orderNum,String resources,Status status,Boolean sys,String title,Type type,SysMenu parent){
        this.genre = genre;
        this.level = level;
        this.orderNum = orderNum;
        this.resources = resources;
        this.status = status;
        this.sys = sys;
        this.title = title;
        this.type = type;
        this.parent = parent;
    }

    @Data
    @ApiModel("菜单展示model")
    public static class Vo implements VO {
        @ApiModelProperty(value = "菜单id")
        private Long id;
        @ApiModelProperty(value = "父菜单id")
        private Long parentId;
        @ApiModelProperty(value = "父菜单名称")
        private String parentTitle;
        @ApiModelProperty(value = "菜单名称")
        private String title;
        @ApiModelProperty(value = "菜单url")
        private String resources;
        @ApiModelProperty(value = "菜单类型")
        private Type type;
        @ApiModelProperty(value = "菜单图标")
        private String icon;
        @ApiModelProperty(value = "排序")
        private Integer orderNum;
        @ApiModelProperty(value = "菜单状态")
        private Status status;
        @ApiModelProperty("菜单层级")
        private Long level;
        @ApiModelProperty("最后修改时间")
        private String lastModifyTime;
        @ApiModelProperty("创建时间")
        private String createTime;
        @ApiModelProperty("子级列表")
        private List<SysMenu.Vo> childs = new ArrayList<>();
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.parentId = this.parent == null ? null : this.parent.id;
        vo.parentTitle = this.parent == null ? "" : this.parent.title;
        vo.title = this.title;
        vo.resources = this.resources;
        vo.type = this.type;
        vo.icon = this.icon;
        vo.orderNum = this.orderNum;
        vo.status = this.status;
        vo.level = this.level;
        vo.lastModifyTime = fmt(this.getLastModifyTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        return vo;
    }
}
